package com.helloandroid.activitys

import android.view.View
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.activity.viewModels
import com.business.Analyse
import com.business.AnalyseKey
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.ads.SmallNativeAdUtil
import com.helloandroid.base.BaseActivity
import com.helloandroid.dialogs.AwardDialog
import com.helloandroid.dialogs.BsTargetDialog
import com.helloandroid.models.butie.BsViewModel
import com.helloandroid.tools.NetWorkUtil
import com.helloandroid.view.DashBoard
import com.helloandroid.vo.BsInfo

class BuShuDaKaActivity : BaseActivity() {

    val model: BsViewModel by viewModels()

    // views
    lateinit var dashBoard: DashBoard
    lateinit var tvCount: TextView
    lateinit var rlTips: RelativeLayout
    lateinit var btnDaka: Button
    lateinit var tvTipText: TextView
    lateinit var tvDesc: TextView
    lateinit var tvTarget: TextView
//    lateinit var bsTargetReceiver: BroadcastReceiver

    var isReady = false
    var mInfo: BsInfo? = null

    override fun getContentViewResId() = R.layout.activity_bushudaka

    override fun initView() {
        // init views
        findBtn(R.id.btnBack).setOnClickListener { finish() }
        dashBoard = findViewById(R.id.dashBoard)
        tvCount = findViewById(R.id.tvCount)
        rlTips = findViewById(R.id.rlTips)
        btnDaka = findViewById(R.id.btnDaka)
        tvTipText = findViewById(R.id.tvTipText)
        tvDesc = findViewById(R.id.tvDesc)
        tvTarget = findViewById(R.id.tvTarget)

        val user = AppUtil.user
        dashBoard.setMaxNum(user.bs_target)
        dashBoard.updateProgress(user.bs)
        tvCount.text = user.bs.toString()
        tvTarget.text = "目标${user.bs_target}步"

        // btn
        findBtn(R.id.btnChgBtn_1).setOnClickListener {
            showChgTargetDialog()
        }
        findBtn(R.id.btnChgBtn_2).setOnClickListener {
            showChgTargetDialog()
        }

        SmallNativeAdUtil.showNativeAd(this, findViewById(R.id.nativeView))
    }

    private fun showChgTargetDialog() {
        if (!isReady) {
            return
        }
        // 弹出框
        val dialog = BsTargetDialog(this)
        dialog.show()
    }

    override fun initData() {
        model.getInfo { success, bsInfo ->
            isReady = success
            if (isReady) {
                mInfo = bsInfo
                updateUI()
            }
        }

//        bsTargetReceiver = BsTargetReceiver()
//        registerReceiver(bsTargetReceiver, IntentFilter(AppConfig.Action_BsOrTarget_Changed))

        model.perSecondTick(this::perSecondTick)
    }

    private fun perSecondTick(){
        if (!isReady){
            return
        }
        val newTarget = AppUtil.user.bs_target
        tvTarget.text = "目标${newTarget}步"
        dashBoard.setMaxNum(newTarget)
        dashBoard.invalidate()

        // 可能这个时候,步数已经达标
        updateUI()
    }

    private fun daka() {
        if (!NetWorkUtil.networkConnected()) {
            AppUtil.toast("请打卡网络.")
            return
        }

        var awardCoin = 50
        AppUtil.appViewModel().buShuDaKa(awardCoin)
        mInfo?.state = 1


        Analyse.report(AnalyseKey.Bs_Daka)

        val dialog = AwardDialog(this, awardCoin)
        dialog.reasonKey = AnalyseKey.Bs_Daka_Video
        dialog.show()

        // 奖励金币,更新UI
        updateUI()
    }

    override fun onDestroy() {
//        unregisterReceiver(bsTargetReceiver)
        super.onDestroy()
    }

    private fun updateUI() {
        val info = mInfo!!
        tvDesc.text = "达标打卡,可领取${info.coin}金币"

        val user = AppUtil.user
        if (user.bs >= user.bs_target && info.state < 1) {
            // 可以打卡
            rlTips.visibility = View.INVISIBLE
            btnDaka.setBackgroundResource(R.drawable.btn_red)
            btnDaka.text = "领取"
            btnDaka.setOnClickListener {
                daka()
            }
        } else {
            // 不可以打卡
            // case1 步数未到
            // case2 步数到了  已经打卡了
            if (user.bs < user.bs_target) {
                rlTips.visibility = View.VISIBLE
                tvTipText.text = "还差${user.bs_target - user.bs}步领取金币"
                btnDaka.text = "打卡"
                btnDaka.setBackgroundResource(R.drawable.btn_blue)
                btnDaka.setOnClickListener {
                    AppUtil.toast("步数未达标")
                }
            } else {
                rlTips.visibility = View.INVISIBLE
                btnDaka.text = "已打卡"
                btnDaka.setBackgroundResource(R.drawable.dd_btn_gray)
                btnDaka.setOnClickListener(null)
            }
        }
    }

//    inner class BsTargetReceiver : BroadcastReceiver() {
//        override fun onReceive(context: Context?, intent: Intent?) {
//            intent?.let {
//                if (it.action == AppConfig.Action_BsOrTarget_Changed) {
//                    MyLog.elog("直接需要更新UI")
//                    val newTarget = MyApplication.user.bs_target
//                    tvTarget.text = "目标${newTarget}步"
//                    dashBoard.setMaxNum(newTarget)
//                    dashBoard.invalidate()
//
//                    // 可能这个时候,步数已经达标
//                    updateUI()
//                }
//            }
//        }
//    }

    override fun isDarkMode() = false
    override fun isUseFullScreenMode() = true
}